/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.XmlDataBase;

import java.io.File;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Adam
 * 
 * !!! JUST FOR NAIVE TESTING !!!
 */
public class Main {
    public static void main(String args[]) {
        
        // Test of ODF transformator
/*          
        MediumManagerImpl manager = new MediumManagerImpl("/db/sample", "admin", "passwd45");
        try {
            Collection<Medium> mediums = new ArrayList<Medium>();
  //          mediums.add(manager.getMediumById("medium.xml")); //type ID of some medium in your collection
    //        mediums.add(manager.getMediumById("medium2.xml"));
            ODFTransformation odf = new ODFTransformation();
 //           odf.exportODF(mediums);
            odf.importODF(new File("media.ods"), manager);
            
        } catch (NullPointerException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (DatabaseException ex) {
//            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
  */     
            
            //Manager handle database operations
            
            Set<Movie> movies = new HashSet<Movie>();
            
            List<String> languages = new ArrayList<String>();
            
            languages.add("sk");
            languages.add("cz");
            movies.add(new Movie("arabela","22:20:10","good","avi","cartoon",languages));
            movies.add(new Movie("Janosik","50:10:07","ok","avi","drama",languages));
            
            Medium newMedium = new Medium();
            newMedium.setName("Yeah");
            newMedium.setType("CD");
            newMedium.setMovies(movies);
            
           
            MediumManagerImpl manager = new MediumManagerImpl("/db/sample", "admin", "YOUR PASSWORD");        
            try {           
                
                // create medium in database
                manager.createMedium(newMedium);
                
                Medium medium = manager.getMediumById(newMedium.getId());
                System.out.println("CREATE MEDIUM:" + medium.toString());
                
                // chaneg medium's paramteres
                medium.setName("Updated");
                medium.setType("DVD");
                medium.addMovie(new Movie("Red","02:30:07","Super","mp4","Comedy",languages));
                
                // if medium already contains the movie, throw InvallidArgument exception
                try{
                medium.addMovie(new Movie("Red","02:30:07","Super","mp4","Comedy",languages));
                }catch(Exception ex){
                    System.out.println("Catched exception:" + ex);
                }
                
                // update medium in database
                manager.updateMedium(medium);           
                System.out.println("UPDATE MEDIUM:" + manager.getMediumById(medium.getId()));
                
                
                Movie bruce = new Movie("Bruce","02:25:04","ko","dvd","Comedy",languages);
                
                // add new movie in medium in database
                manager.appendMovie(medium,bruce);          
                System.out.println("ADD MOVIE1: " + manager.getMediumById(medium.getId()));
                
                // should throw exception > the movie with the same name is in medium already
                try{
                    manager.appendMovie(medium, bruce);
                    System.out.println("ADD MOVIE2: " + manager.getMediumById(medium.getId()));
                }catch(IllegalArgumentException ex){
                    System.out.println("ADD MOVIE2" + ex.getMessage());
                }
                
                // removie movie from medium in database
                manager.removeMovie(medium, bruce);
                System.out.println("REMOVE MOVIE: " + manager.getMediumById(medium.getId()));
                
                // get all mediums movies in database
                Set<Movie> moviesSet = manager.getMediumsMovies(medium);
                System.out.println("MEDIUM'S MOVIES:" + moviesSet);
                
                // delete medium from database
              /*  manager.deleteMedium(medium.getId()); 
                Medium med = manager.getMediumById(medium.getId());
                if(med == null)
                    System.out.println("DELETE MEDIUM: OK > " + medium.getId() + " deleted");*/
                
                // get all mediums ids in database
               List<String> mediumsIds = manager.getAllMediumsIds();
                if(mediumsIds != null)
                    System.out.println("MEDIUMS IDS:" + mediumsIds);
                else
                    System.out.println("No mediums in database!");
                
                System.out.println("Mediums with movie " + "Red :" + manager.getMediumsWithMovie("Red"));
                
            } catch (DatabaseException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NullPointerException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            

        
    }
}
